package com.kakaxi.mybilibili.module.home.drama.presenter;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.kakaxi.mybilibili.module.home.drama.model.DramaModel;
import com.kakaxi.mybilibili.module.home.drama.model.IDramaModel;
import com.kakaxi.mybilibili.module.home.drama.view.IDramaView;
import com.kakaxi.mybilibili.bean.BannerItemBean;
import com.kakaxi.mybilibili.bean.DramaRecommendItemBean;
import com.kakaxi.mybilibili.bean.DramaTotalItemBean;
import com.kakaxi.mybilibili.bean.SerializingItemBean;
import com.kakaxi.mybilibili.common.WebParamConst;
import com.kakaxi.mybilibili.utils.LogUtil;
import com.vincestyling.netroid.Listener;
import com.vincestyling.netroid.NetroidError;

import java.util.List;

/**
 * 番剧主页面控制层实现类
 * Created by kakaxi on 16/10/20.
 */

public class DramaPresenter implements IDramaPresenter {

    /** 番剧数据层 **/
    private IDramaModel mDramaModel;

    /** 番剧视图层 **/
    private IDramaView mDramaView;

    public DramaPresenter(IDramaView dramaView) {
        mDramaModel = new DramaModel();
        mDramaView = dramaView;
    }

    @Override
    public void initDramaSerializaingData() {
        // 设置刷新后再获取数据
        mDramaView.showRefresh();
        getDramaSerializingData();
    }

    @Override
    public void refreshDramaSerializaingData() {
        getDramaSerializingData();
    }

    @Override
    public void initDramaRecommendData() {

    }

    @Override
    public void refreshDramaRecomendData() {

    }

    @Override
    public void loadMoreDramaRecommendData(int startIndex) {
        mDramaModel.getMoreRecommendData(new Listener<JSONObject>() {
            @Override
            public void onSuccess(JSONObject response) {
                super.onSuccess(response);
                // 判断响应结果
                if (response.getString(WebParamConst.RESPONSE_CODE).equals(WebParamConst
                        .RESULT_CODE_OK)) { // 更多推荐获取成功
                    // 提取出推荐数组转化为信息列表, 最后添加到总信息类中
                    List<DramaRecommendItemBean> dramaRecommendItemBeanList =
                            toRecommendBeanList(response);
                    mDramaView.addDramaRecommendData(dramaRecommendItemBeanList);
                } else {

                }
            }

            @Override
            public void onError(NetroidError error) {
                super.onError(error);
            }
        });
    }

    /**
     * 做获取连载番剧数据的操作
     */
    private void getDramaSerializingData() {
        mDramaModel.getSerializingData(new DramaRequestListener());
    }

    /**
     * @param dataJsonObj
     * @return
     */
    private DramaTotalItemBean toDramaSerializingBean(JSONObject dataJsonObj) {
        // 新建总的信息类
        DramaTotalItemBean dramaTotalItemBean = new DramaTotalItemBean();

        // 转换成广告数据列表
        String bannerStr = dataJsonObj.getJSONObject(WebParamConst.RESPONSE_DATA_DRAMA_AD)
                .getJSONArray(WebParamConst.RESPONSE_DATA_DRAMA_HEAD).toString();
        List<BannerItemBean> bannerList = JSON.parseArray(bannerStr, BannerItemBean.class);
        dramaTotalItemBean.setBannerItemList(bannerList);

        // 转换成最新连载数据列表
        String newSerializingStr = dataJsonObj.getJSONArray(WebParamConst
                .RESPONSE_DATA_DRAMA_SERIALIZING)
                .toString();
        List<SerializingItemBean> newSerializingList = JSON.parseArray(newSerializingStr,
                SerializingItemBean.class);
        dramaTotalItemBean.setNewSerializingItemList(newSerializingList);

        // 转换成上一季度连载数据列表
        String lastSerializingStr = dataJsonObj.getJSONObject(WebParamConst
                .RESPONSE_DATA_DRAMA_PREVIOUS).getJSONArray(WebParamConst
                .RESPONSE_DATA_DRAMA_LIST).toString();
        List<SerializingItemBean> lastSerializingList = JSON.parseArray(lastSerializingStr,
                SerializingItemBean.class);
        dramaTotalItemBean.setLastSerializingItemList(lastSerializingList);

        return dramaTotalItemBean;
    }

    /**
     * 转换成推荐信息数组
     *
     * @param responseJsonObj 响应json对象数据
     * @return 推荐信息数组
     */
    private List<DramaRecommendItemBean> toRecommendBeanList(JSONObject responseJsonObj) {
        String resultArrStr = responseJsonObj.getJSONArray(WebParamConst
                .RESPONSE_RESULT).toString();
        return JSON.parseArray(resultArrStr, DramaRecommendItemBean.class);
    }

    /**
     * 连载请求回调监听器
     */
    private class DramaRequestListener extends Listener<JSONObject> {

        @Override
        public void onSuccess(JSONObject response) {
            super.onSuccess(response);
            // 判断响应结果
            if (response.getString(WebParamConst.RESPONSE_CODE).equals(WebParamConst
                    .RESULT_CODE_OK)) { // 番剧连载数据获取成功
                LogUtil.d("番剧连载获取数据成功!");
                // 把json数据转换成总的信息类
                final DramaTotalItemBean dramaTotalItemBean = toDramaSerializingBean
                        (response.getJSONObject(WebParamConst
                                .RESPONSE_RESULT));

                mDramaModel.getRecommendData("-1", "10", new Listener<JSONObject>() {
                    @Override
                    public void onSuccess(JSONObject response) {
                        super.onSuccess(response);
                        mDramaView.hideRefresh();

                        if (response.getString(WebParamConst.RESPONSE_CODE).equals
                                (WebParamConst.RESULT_CODE_OK)) { // 推荐番剧获取成功
                            // 提取出推荐数组转化为信息列表, 最后添加到总信息类中
                            List<DramaRecommendItemBean> dramaRecommendItemBeanList =
                                    toRecommendBeanList(response);
                            dramaTotalItemBean.setDramaRecommendItemList
                                    (dramaRecommendItemBeanList);
                        } else { // 推荐番剧获取失败

                        }
                        // 展示数据
                        mDramaView.showDramaTotalData(dramaTotalItemBean);
                    }

                    @Override
                    public void onError(NetroidError error) {
                        super.onError(error);
                        mDramaView.hideRefresh();
                        mDramaView.showDramaTotalData(dramaTotalItemBean);
                    }
                });

            } else { // 番剧连载数据获取失败
                mDramaView.hideRefresh();
            }
        }

        @Override
        public void onError(NetroidError error) {
            super.onError(error);
            mDramaView.hideRefresh();
        }
    }

}
